package com.ohos.carbon.slice.dialog;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.window.dialog.ListDialog;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class SelectDialogActivity extends AbilitySlice {

    public Image back_button, image1, image2;
    public Text text_title;
    private int index;
    boolean[] status = new boolean[5];


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_selectdialog);
        final TextField titleText = (TextField) findComponentById(ResourceTable.Id_titleText4);
        initView();

        Button button1 = (Button) findComponentById(ResourceTable.Id_dropDown1);
        Button button2 = (Button) findComponentById(ResourceTable.Id_dropDown2);
        Button button3 = (Button) findComponentById((ResourceTable.Id_dropDown3));
        Component componentById = findComponentById(ResourceTable.Id_hidelayout);

        button1.setClickedListener(component -> {
            componentById.setVisibility(Component.VISIBLE);
            button1.setVisibility(Component.INVISIBLE);
        });

        button2.setClickedListener(component -> {
            componentById.setVisibility(Component.INVISIBLE);
            button1.setVisibility(Component.VISIBLE);
            button1.setText(button2.getText());
            Button button = (Button) findComponentById(ResourceTable.Id_button);
            button.setClickedListener(component1 -> {
                ListDialog listDialog = new ListDialog(getContext());
                listDialog.setContentCustomComponent(component1);
                String[] selectitem = {"Darian", "Grapefruit", "Purple mangosteen", "Yuzu", "Cranberry"};
                listDialog.setSingleSelectItems(selectitem, index);
                listDialog.setTitleText(titleText.getText());
                listDialog.setOnSingleSelectListener((iDialog, i) -> {
                    switch (i) {
                        case 0:
                            index = i;
                            listDialog.destroy();
                            break;
                        case 1:
                            index = i;
                            listDialog.destroy();
                            break;
                        case 2:
                            index = i;
                            listDialog.destroy();
                            break;
                        case 3:
                            index = i;
                            listDialog.destroy();
                            break;
                        case 4:
                            index = i;
                            listDialog.destroy();
                            break;
                    }
                });
                listDialog.show();
            });
        });
        button3.setClickedListener(component -> {
            componentById.setVisibility(Component.INVISIBLE);
            button1.setVisibility(Component.VISIBLE);
            button1.setText(button3.getText());
            Button button = (Button) findComponentById(ResourceTable.Id_button);
            button.setClickedListener(component12 -> {
                ListDialog listDialog = new ListDialog(getContext());
                listDialog.setContentCustomComponent(component12);
                String[] selectitem = {"Darian", "Grapefruit", "Purple mangosteen", "Yuzu", "Cranberry"};
                listDialog.setMultiSelectItems(selectitem, status);
                listDialog.setTitleText(titleText.getText());
                listDialog.setOnMultiSelectListener((iDialog, i, b) -> status[i] = b);
                listDialog.setButton(1, "OK", (iDialog, i) -> listDialog.destroy());
                listDialog.show();
            });
        });

        Button button = (Button) findComponentById(ResourceTable.Id_button);
        button.setClickedListener(component -> {
            ListDialog listDialog = new ListDialog(getContext());
            listDialog.setContentCustomComponent(component);
            String[] selectitem = {"Darian", "Grapefruit", "Purple mangosteen", "Yuzu", "Cranberry"};
            listDialog.setSingleSelectItems(selectitem, index);
            listDialog.setTitleText(titleText.getText());
            listDialog.setOnSingleSelectListener((iDialog, i) -> {
                switch (i) {
                    case 0:
                        index = i;
                        listDialog.destroy();
                        break;
                    case 1:
                        index = i;
                        listDialog.destroy();
                        break;
                    case 2:
                        index = i;
                        listDialog.destroy();
                        break;
                    case 3:
                        index = i;
                        listDialog.destroy();
                        break;
                    case 4:
                        index = i;
                        listDialog.destroy();
                        break;
                }
            });
            listDialog.show();
        });

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Select dialog")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Select dialog")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Select dialog", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Select dialog", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Select dialog", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.VISIBLE);
        image2.setVisibility(Component.VISIBLE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Select dialog");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
